package com.ms.action.basic;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eddy.util.AjaxOut;
import com.ms.dao.entity.basic.ResourcesEty;
import com.ms.dao.entity.basic.UserEty;
import com.ms.dao.entity.basic.UserRoleEty;
import com.ms.dao.entity.basic.UserShortcutsEty;
import com.ms.dao.mapper.basic.RolePermissionDao;
import com.ms.dao.mapper.basic.UserDao;
import com.ms.dao.mapper.basic.UserShortcutsDao;

@Controller
@RequestMapping("/HTML/basic/LoginAction")
public class LoginAction {
	private Logger logger = Logger.getLogger(LoginAction.class);
	
	@Resource(name="UserDao")
	private UserDao userDao;
	
	@Autowired
	private RolePermissionDao rolePermissionDao;
	
	@Autowired
	private UserShortcutsDao userShortcutsDao;
	
	@RequestMapping("/login.action")
	public void login(HttpServletRequest request, HttpServletResponse response, UserEty userEty) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			
			String validateCode = request.getParameter("validateCode");
			String validateCodeSeesion = (String) request.getSession().getAttribute("validateCode");
			if(!validateCode.equals(validateCodeSeesion)) {
				obj.put("result","fail");
				obj.put("info","验证码不正确！");
			}
			else {
				List list =  userDao.selectByEntity(userEty);
				if(list.size() == 1) {
					userEty = (UserEty) list.get(0);
					request.getSession().setAttribute("UserEty", userEty);
					obj.put("result","success");
				}
				else {
					obj.put("result","fail");
					obj.put("info","用户名或者密码错误！");
				}
			}
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	@RequestMapping("/logout.action")
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			request.getSession().removeAttribute("UserEty");
			request.getSession().invalidate();
			obj.put("result","success");
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	/**
	 * 得到登录用户的所有信息，资源 以及权限
	 */
	@RequestMapping("/getUserAllTree.action")
	public void getUserAllTree(HttpServletRequest request, HttpServletResponse response) {
		JSONObject userDataObj = new JSONObject();
		try {
			UserEty userEty = (UserEty)request.getSession().getAttribute("UserEty");
			userDataObj.put("userInfo", userEty);
			
			UserRoleEty ety = new UserRoleEty();
			ety.setUserId(userEty.getId());
			ety.setId(0);//用于存放parentNodeId
			JSONArray array = new JSONArray();
			List<ResourcesEty> buttons = userDao.selectUserTree(ety);
			for(int i = 0; i < buttons.size(); i++) {
				ResourcesEty recety = buttons.get(i);
				JSONObject menu = new JSONObject();
				menu.put("id", recety.getId());
				menu.put("icon", recety.getIcon());
				menu.put("nodeId", recety.getNodeId());
				menu.put("menuName", recety.getMenuName());
				if(recety.getActionPath() == null || recety.getActionPath().trim().equals("")) {
					menu.put("isLeaf", false);
					
					ety.setId(recety.getNodeId());
					List<ResourcesEty> childNodes = userDao.selectUserTree(ety);
					JSONArray items = new JSONArray();
					for(int k = 0; k < childNodes.size(); k++) {
						ResourcesEty itemEty = childNodes.get(k);
						JSONObject menuItem = new JSONObject();
						menuItem.put("id", itemEty.getId());
						menuItem.put("nodeId", itemEty.getNodeId());
						menuItem.put("menuName", itemEty.getMenuName());
						menuItem.put("url", itemEty.getActionPath());
						items.add(menuItem);
					}
					
					menu.put("items", items);
				}
				else {
					menu.put("isLeaf", true);
					menu.put("url", recety.getActionPath());
				}
				array.add(menu);
			}
			userDataObj.put("userTree", array);
			
			UserShortcutsEty userShortcutsEty = new UserShortcutsEty();
			userShortcutsEty.setUserId(userEty.getId());
			List list = userShortcutsDao.selectByEntity(userShortcutsEty);
			JSONArray shortcutsArr = new JSONArray();
			shortcutsArr.addAll(list);
			userDataObj.put("userShortcuts", shortcutsArr);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		AjaxOut.responseText(response, userDataObj.toString());
	}
	
	@RequestMapping("/getUserPermission.action")
	public void getUserPermission(HttpServletRequest request, HttpServletResponse response) {
		try {
			UserEty userEty = (UserEty)request.getSession().getAttribute("UserEty");
			List plist = rolePermissionDao.selectUserPermission(userEty.getId());
			JSONObject obj = new JSONObject();
			obj.put("UserInfo", userEty);
			obj.put("Permission", plist);
			AjaxOut.responseText(response, obj.toString());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
